package com.maxdu.labs.stt.models.db;

import android.util.Log;

import com.maxdu.labs.stt.constants.Constants;
import com.maxdu.labs.stt.models.UserVSTPlan;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.SaveCallback;

public class UserVSTPlanDBHelper {
	public static final String PARSE_DB_TABLENAME_USERVSTPLAN = "UserVSTPlan";
	public static final String PARSE_DB_FIELD_TPLANID = "tplanId";
	public static final String PARSE_DB_FIELD_USERID = "userId";
	public static final String PARSE_DB_FIELD_ROLE = "role";
	public static final String PARSE_DB_FIELD_ACCEPTED = "accepted";
	
	private ParseObject userVSTrainingParseObject;
	//private TPlan training;
	
	public UserVSTPlanDBHelper(){
		userVSTrainingParseObject = new ParseObject(PARSE_DB_TABLENAME_USERVSTPLAN);
	}
	
	public void saveToCloud(final UserVSTPlan userVSTPlan){

		if(userVSTPlan != null){
			userVSTrainingParseObject.put(PARSE_DB_FIELD_TPLANID, userVSTPlan.getTrainingId());
			userVSTrainingParseObject.put(PARSE_DB_FIELD_USERID, userVSTPlan.getUserId());
			userVSTrainingParseObject.put(PARSE_DB_FIELD_ROLE, userVSTPlan.getRole());
			userVSTrainingParseObject.put(PARSE_DB_FIELD_ACCEPTED, userVSTPlan.getAccepted());
			
			userVSTrainingParseObject.saveInBackground(new SaveCallback() {
				
				@Override
				public void done(ParseException e) {
					if (e == null){
						Log.d(Constants.LOG_TAG, "we successfully updated to Cloud the UserVSTPlan: TPlan [" + userVSTPlan.getTrainingId() + "] - User [" +userVSTPlan.getUserId() + "]" );
					}else{
						Log.w(Constants.LOG_TAG, "PROBLEM when trying to save the the UserVSTPlan: TPlan [" + userVSTPlan.getTrainingId() + "] - User [" +userVSTPlan.getUserId() + "] to Cloud. errorMessage:: " + e);
					}
					
				}
			});
		}else {
			Log.e(Constants.LOG_TAG, "UserVSTrainingDBHelper::saveToCloud() :: userVSTraining object IS NULL");
		}
	}
}
